<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of Melange_Framework
 *
 * PHP-HTTP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-HTTP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-HTTP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package aas
 *
 */

/**
 * SavedRequest class
 *
 * Object that saves the critical information from a request so that
 * form-based authentication can reproduce it once the user has been
 * authenticated.
 * <p>
 * <b>IMPLEMENTATION NOTE</b> - It is assumed that this object is accessed
 * only from the context of a single thread, so no synchronization around
 * internal collection classes is performed.
 * <p>
 * <b>FIXME</b> - Currently, this object has no mechanism to save or
 * restore the data content of the request, although it does save
 * request parameters so that a POST transaction can be faithfully
 * duplicated.
 *
 * @category    Melange Framework
 * @package     aas
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class SavedRequest {


    /**
     * The set of Cookies associated with this Request.
     */
    private $cookies = array();


    public function addCookie($cookie) {
        $this->cookies[] = $cookie;
    }

    public function getCookies() {
        return ($this->cookies);
    }


    /**
     * The set of Headers associated with this Request.  Each key is a header
     * name, while the value is a ArrayList containing one or more actual
     * values for this header.  The values are returned as an Iterator when
     * you ask for them.
     */
    private $headers = array();

    public function addHeader($name, $value) {
        $values = array_values($this->headers[$name]);
        if ($values === null) {
            $values = array();
            $this->headers[$name] =& $values;
        }
        $values[] = $value;
    }

    public function getHeaderNames() {
        return array_keys($this->headers);
    }

    public function getHeaderValues($name) {
        $values = array_values($this->headers[$name]);
        if ($values === null)
            return array();
        else
            return $values;
    }


    /**
     * The set of Locales associated with this Request.
     */
    private $locales = array();

    public function addLocale(Locale $locale) {
        $this->locales[] = $locale;
    }

    public function getLocales() {
        return ($this->locales);
    }


    /**
     * The request method used on this Request.
     */
    private $method = null;

    public function getMethod() {
        return ($this->method);
    }

    public function setMethod($method) {
        $this->method = $method;
    }



    /**
     * The set of request parameters associated with this Request.  Each
     * entry is keyed by the parameter name, pointing at a String array of
     * the corresponding values.
     */
    private $parameters = array();

    public function addParameter($name, $values) {
        $this->parameters[$name] = $values;
    }

    public function getParameterNames() {
        return array_keys($this->parameters);
    }

    public function getParameterValues($name) {
        return $this->parameters[$name];
    }


    /**
     * The query string associated with this Request.
     */
    private $queryString = null;

    public function getQueryString() {
        return ($this->queryString);
    }

    public function setQueryString($queryString) {
        $this->queryString = $queryString;
    }


    /**
     * The request URI associated with this Request.
     */
    private $requestURI = null;

    public function getRequestURI() {
        return ($this->requestURI);
    }

    public function setRequestURI($requestURI) {
        $this->requestURI = $requestURI;
    }


    public function __toString() {

        $sb = "[SavedRequest";


        $sb .= "\n\tparameters=(";
        $parameters = $this->getParameterNames();
        for($i=0; $i < count($parameters); $i++) {

            $sb .= "\n\t\t".$parameters[$i]."=".strval($this->getParameter($parameters[$i]));
            if($i < count($parameters) - 1)
                $sb .= ",";
        }
        $sb .= "\n\t)";
        $sb .= ",\n\tqueryString=".$this->getQueryString();
        $sb .= ",\n\trequestURI=".$this->getRequestURI();

        $sb .= "\n]";

        return $sb;

    }

}

?>